package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.dao.RackConfigMetaDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.RackConfigMetaEntity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RackConfigMetaImpl implements RackConfigMetaDao {

    @Override
    public RackConfigMetaEntity findOneByCabinet(String cabinetSn) {
        String sql = "select * from rack_config_meta where rack_code = ? ";
        String[] params = new String[1];
        params[0] = cabinetSn;
        RackConfigMetaEntity result = JdbcUtil.queryForBean(sql, params, RackConfigMetaEntity.class);
        return result;
    }

    @Override
    public boolean insert(RackConfigMetaEntity rackConfigMeta) throws SQLException {
        String sql = "INSERT INTO rack_config_meta(gmt_create,gmt_modified,rack_code,meta) VALUES (?,?,?,?)";
        List<Object> objects = new ArrayList<>();
        objects.add(rackConfigMeta.getGmt_create());
        objects.add(rackConfigMeta.getGmt_modified());
        objects.add(rackConfigMeta.getRack_code());
        objects.add(rackConfigMeta.getMeta());
        Object[] params = objects.toArray();
        return JdbcUtil.update1(sql, params);
    }

    @Override
    public boolean update(RackConfigMetaEntity rackConfigMeta) throws SQLException {
        String sql = "UPDATE rack_config_meta set gmt_create=?,gmt_modified=?,rack_code=?,meta=? WHERE id = ?";
        List<Object> objects = new ArrayList<>();
        objects.add(rackConfigMeta.getGmt_create());
        objects.add(rackConfigMeta.getGmt_modified());
        objects.add(rackConfigMeta.getRack_code());
        objects.add(rackConfigMeta.getMeta());
        objects.add(rackConfigMeta.getId());
        Object[] params = objects.toArray();
        return JdbcUtil.update1(sql, params);
    }

    @Override
    public boolean delete(String cabinetSn) throws SQLException {
        String sql = "DELETE FROM rack_config_meta WHERE rack_code = ?";
        Object[] params = new Object[1];
        params[0] = cabinetSn;
        return JdbcUtil.update1(sql, params);
    }

    @Override
    public boolean delete(String[] cabinetSns) throws SQLException {
        int length = cabinetSns.length;
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("DELETE FROM rack_config_meta WHERE rack_code IN ");
        sqlBuffer.append("(");
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                sqlBuffer.append("?)");
            } else {
                sqlBuffer.append("?,");
            }
        }
        String sql = sqlBuffer.toString();
        boolean result = JdbcUtil.update1(sql, cabinetSns);
        return result;
    }
}
